<template>
  <div class="header1">
    <!-- 折叠按钮 @click="collapseChage"-->
    <div class="collapse-btn1">
      <!-- <i v-if="!collapse" class="el-icon-s-fold"></i>
      <i v-else class="el-icon-s-unfold"></i> -->
      <img class="logoicon" src="../assets/img/logo1.png" />
      <div class="logo">{{ platformName }}</div>
      <!-- <div class="logo">生态资源申报流转平台</div> -->
    </div>
    <div class="header-right">
      <div class="header-user-con">
        <!-- 消息中心 -->
        <router-link to="/Myfeedback">
          <span class="fontSize14 el-dropdown-link marginrRight20">我的反馈</span>
        </router-link>
        <div class="btn-bell">
          <!-- <el-tooltip
            effect="dark"
            :content="message ? `有${message}条未读消息` : `消息中心`"
            placement="bottom"
          >
            <router-link to="/tabs">
              <i class="el-icon-bell"></i>
            </router-link>
          </el-tooltip> -->

          <router-link to="/tabs">
            <i class="el-icon-bell"></i>
          </router-link>
          <span class="btn-bell-badge" v-if="message"></span>
        </div>
        <!-- 用户头像 -->
        <div class="user-avator">
          <img src="../assets/img/head_portrait.png" />
        </div>
        <!-- 用户名下拉菜单 -->
        <div v-if="username">
          <el-dropdown class="user-name" trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              {{ username }}
              <i class="el-icon-caret-bottom"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <!-- <el-dropdown-item command="user">个人信息</el-dropdown-item> -->
                <!-- <el-dropdown-item command="identity">身份认证</el-dropdown-item> -->
                <!-- <el-dropdown-item command="passwd">修改密码</el-dropdown-item> -->
                <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div v-else class="login_ cursorp" @click="handleLogin">登录</div>
      </div>
    </div>
  </div>
</template>
<script>
import { computed, onMounted } from "vue";
import { ref, reactive, getCurrentInstance } from "vue";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import api from "../api/api";
export default {
  setup() {
    let { proxy } = getCurrentInstance();
    const platformName = ref("");
    proxy.$store.dispatch('getplatformName').then((res) => {
      platformName.value = res;
    })
    const username = ref("");
    const message = 2;
    const getuserinfo = () => {
      api
        .getListAPI("auth/client/account/loginAccountInfo", {})
        .then((res) => {
          console.log(res);
          if (res.data.code == 1) {
            console.log(res.data.data);
            username.value =
              res.data.data.accountInfoDTO.realName ||
              res.data.data.accountInfoDTO.phone;
            localStorage.setItem(
              "ms_username",
              res.data.data.accountInfoDTO.realName
            );
            var userInfo = JSON.stringify(res.data.data.accountInfoDTO);
            localStorage.setItem("userInfo", userInfo);
            if (res.data.data.IsAuth == 0) {
              router.push("/user");
            }
          } else if (res.data.code == 401) {
            // router.push("/login");
            ElMessage.error("请重新登录");
          } else {
            // router.push("/login");
            // ElMessage.error(res.data.msg);
          }
          //数据处理
        })
        .catch((err) => {
          console.log(err);
        });
    };
    getuserinfo();
    const store = useStore();
    const collapse = computed(() => store.state.collapse);
    // 侧边栏折叠
    const collapseChage = () => {
      store.commit("handleCollapse", !collapse.value);
    };

    onMounted(() => {
      if (document.body.clientWidth < 1500) {
        collapseChage();
      }
    });

    // 用户名下拉菜单选择事件
    const router = useRouter();
    const handleCommand = (command) => {
      if (command == "loginout") {
        localStorage.removeItem("token");
        localStorage.removeItem("userInfo");
        localStorage.clear();
        router.push("/login");
      } else if (command == "user") {
        router.push("/user");
      } else if (command == "passwd") {
        router.push("/passwd");
      } else if (command == "identity") {
        router.push("/identity");
      }
    };
    const handleLogin = () => {
      router.push("/login");
    };
    return {
      username,
      message,
      collapse,
      platformName,
      collapseChage,
      getuserinfo,
      handleCommand,
      handleLogin,
    };
  },
};
</script>
<style scoped>
@import "../assets/css/common.css";

.header1 {
  position: relative;
  box-sizing: border-box;
  /* width: 90%; */
  height: 70px;
  font-size: 22px;
  color: #fff;
  margin: 0 auto;
  background-color: #01957d;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.collapse-btn1 {
  float: left;
  padding: 0 21px;
  cursor: pointer;
  line-height: 70px;
  display: flex;
  align-items: center;
}

.collapse-btn1 .logo {
  float: left;
  /* width: 250px; */
  line-height: 70px;
  margin-left: 10px;
}

.header-right {
  float: right;
  padding-right: 50px;
}

.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}

.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}

.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}

.btn-bell-badge {
  position: absolute;
  right: 0;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
  color: #fff;
}

.btn-bell .el-icon-bell {
  color: #fff;
}

.user-name {
  margin-left: 10px;
}

.user-avator {
  margin-left: 20px;
  margin-right: 20px;
}

.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}

.el-dropdown-menu__item {
  text-align: center;
}

.logoicon {
  width: 30px;
  margin-left: 30px;
}

.SelectPattern {
  width: 120px;
  margin: 0 10px;
}
</style>
